﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PClusterModel.Infrastructure;

namespace PClusterModel.Cluster
{
    internal class MaximalDimensionSetFinderParallelScaling:MaximalDimensionSetFinderScaling
    {

        public MaximalDimensionSetFinderParallelScaling(PClusterizer clusterizer)
            : base(clusterizer)
        { }

        public override void CalculateMaximalDimensionSetsAndPlaceIntoBlocks()
        {
            int objectCount = this.clusterizer.DataSet.NumberOfDataObjects;
            object sync = new object();
            Parallel.For(0, objectCount - 1, i =>
            {
                for (int j = i + 1; j < objectCount; j++)
                {
                    IMicroarrayDataObject firsIMicroarrayDataObject = this.clusterizer.DataSet.MicroarrayDataObjects[i];
                    IMicroarrayDataObject secondObject = this.clusterizer.DataSet.MicroarrayDataObjects[j];

                    MaximalDimensionBlockSet maximalDimensionSet = this.FindMaximalDimensionSet(firsIMicroarrayDataObject, secondObject);
                    if (maximalDimensionSet != null)
                    {
                        lock (sync)
                        {
                            this.PlaceMaximalDimensionSetInBlock(maximalDimensionSet, firsIMicroarrayDataObject);
                            this.PlaceMaximalDimensionSetInBlock(maximalDimensionSet, secondObject);
                        }
                    }
                }
            });
        }
    }
}
